class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        sort(piles.begin(), piles.end());
        long long n = piles.size();
        long long left = 1;
        long long right = piles[n - 1];
        int mid = 0;
        if (h == piles.size()) {
            return piles[n - 1];
        }
        else {
            long long count = 0;
            long long ans = 0;
            while (left <= right) {
                // mid = left + (right-left)/2;
                mid = (right + left) / 2;
                for (int i = 0; i < n; i++) {
                    if (piles[i] % mid != 0) {
                        count += piles[i] / mid + 1;
                    }
                    else {
                        count += piles[i] / mid;
                    }
                }
                if (count > h) {
                    left = mid + 1;
                    ans = mid;
                }
                else {
                    right = mid - 1;
                }
                count = 0;
            }
            return ans + 1;
        }
    }
};